package com.zonekey.config;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @description: 在endpoints.tokenEnhancer(new AirForceAccessTokenConverter())中，拓展token的返回只
 * @author: 靳俊英
 * @create: 2021-05-18 18:03
 **/
public class AirForceAccessTokenConverter implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
        Set<String> scope = accessToken.getScope();
        Map<String, Object> info = new LinkedHashMap<String, Object>(accessToken.getAdditionalInformation());
        // 给某些特定的scope拓展返回值
        if(scope.contains("air-force")){
            //添加额外需要返回的字段 info
            //info.put("refresh_token",accessToken.getRefreshToken().getValue());
            info.put("tenant_id","success");
            info.put("license","success");
            info.put("pc_template","success");
            info.put("user_type","success");
            info.put("channel_id","success");
            info.put("user_id","success");
            info.put("user_realname","success");
            info.put("username","success");
        }
        info.put("author","靳");
        result.setAdditionalInformation(info);
        return result;
    }
}
